package com.wwwarehouse.resource_center.fragment.createobject.creategoods;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.animation.Animation;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.adapter.createobject.ChooseCategoryAdapter;
import com.wwwarehouse.resource_center.bean.goods.CategoryBean;
import com.wwwarehouse.resource_center.constant.ResourceConstant;
import com.wwwarehouse.resource_center.eventbus_event.CategoryEvent;
import com.wwwarehouse.resource_center.eventbus_event.RefreshEvent;
import com.wwwarehouse.resource_center.fragment.rules.TheParentFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * @author Joseph on 2017/9/14.
 *         <p/>
 */

public class CategoryFragment extends TheParentFragment {

    private RecyclerView mRvContent;
    ChooseCategoryAdapter mAdapter;
    private List<CategoryBean.EmptyIdentifierBean> tagBeanList = new ArrayList<>();
    private int position;
    private PopupWindow popupWindow;

    @Override
    protected int getContentId() {
        return R.layout.fragment_rule_select_application_objectst;
    }

    @Override
    public void onSuccess(CommonClass commonClass, int i) {
        if (i == 0) {
            if (TextUtils.equals("0", commonClass.getCode())) {
                EventBus.getDefault().post(new RefreshEvent("refresh"));
            } else {
                toast(commonClass.getMsg());
            }
        }

    }

    @Override
    public void onReLoad(int code) {

    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    @Override
    protected void init(View view) {
        mRvContent = findView(view, R.id.rv_content);

    }

    @Override
    protected void loadDatas() {
        Bundle b = getArguments();
        List<CategoryBean.EmptyIdentifierBean> taskUnits = b.getParcelableArrayList("data");
        int pos = b.getInt("pos");
        int num = b.getInt("num");
        for (int i = pos * num; i < num + pos * num; i++) {
            if (i >= 0 && i < taskUnits.size()) {
                tagBeanList.add(taskUnits.get(i));
            }
        }
        mRvContent.setLayoutManager(new LinearLayoutManager(mActivity) {
            @Override
            public boolean canScrollHorizontally() {
                return false;
            }

            @Override
            public boolean canScrollVertically() {
                return false;
            }

        });
        if (mAdapter == null) {
            mAdapter = new ChooseCategoryAdapter(mActivity, tagBeanList);
            mAdapter.setOnItemLongClickListener(new ChooseCategoryAdapter.OnItemLongclickListener() {
                @Override
                public void onLongClick(View view, int position) {
                    shwoPopupWindow(view, position);
                }
            });
        }
        mRvContent.setAdapter(mAdapter);
    }


    private void shwoPopupWindow(View view, final int position) {
        this.position = position;
        //用来存放当前的item的坐标值，第一个是x的坐标，第二个是y的坐标
        int[] location = new int[2];
        //把当前的item的坐标放到int数组里面
        view.getLocationInWindow(location);
        View popupView = View.inflate(mActivity, R.layout.add_or_delete_category, null);
        //new 一个PopupWindow出来
        popupWindow = new PopupWindow(popupView, view.getWidth(), view.getHeight());
        //一定要给popupWindow设置一个背景图片，不然的话，会有很多未知的问题的
        //如没办法给它加上动画，还有显示会有问题等。
        //如果我们没有要设置的图片，那么我们就给它加上一个透明的背景图片
//        Drawable drawable = new ColorDrawable(Color.TRANSPARENT);
        LinearLayout mAddCategory = (LinearLayout) popupView.findViewById(R.id.add_category);
        LinearLayout mDeleteCategory = (LinearLayout) popupView.findViewById(R.id.delete_category);
        mAddCategory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EventBus.getDefault().post(new CategoryEvent(tagBeanList.get(position), "add"));
                if (popupWindow != null && popupWindow.isShowing())
                    popupWindow.dismiss();
            }
        });

        mDeleteCategory.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (popupWindow != null && popupWindow.isShowing())
                    popupWindow.dismiss();
                deleteCategory(position);

            }
        });
//        popupWindow.setBackgroundDrawable(drawable);
        popupWindow.setOutsideTouchable(true);
        int x = location[0];
        int y = location[1];
        //把popupWindow显示出来
        popupWindow.showAtLocation(view, Gravity.LEFT | Gravity.TOP, x, y);
    }

    private void deleteCategory(int position) {

        Map<String, Object> map = new HashMap<>();
        map.put("categoryUkid", tagBeanList.get(position).getCategoryUkid());
        httpPost(ResourceConstant.DELETE_CATEGORY, map, 0, true, "");

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (popupWindow != null && popupWindow.isShowing())
            popupWindow.dismiss();
    }
}
